(function () {
    function KeyStorage(key) {
        if (!key || key.constructor != String) throw "传入该库的参数";  //传入该库的参数
        this.key = key;
        var key_now = "theID_"+ key ;
        if(localStorage[key_now])
            this.theID =parseInt(localStorage[key_now])
        else{
            localStorage[key_now]= this.theID =   0; //当前
        }
    }
    KeyStorage.prototype.forEach = function (call) {
        for (var lkey in localStorage) {
            if (lkey.substr(0,this.key.length)===this.key) {
                try {
                    var tem = JSON.parse(localStorage.getItem(lkey) )
                    call(tem)
                } catch (e) {
                    console.log("处理" + id + "出错" + e);
                }
            }
        }
    };
    KeyStorage.prototype.getNullID = function () {
      let id = 0;
      while (localStorage.hasOwnProperty(this.key + id)) {
          id++;
      }
      return id;
  };
    KeyStorage.prototype.getNextId = function () {
        let id = this.theID || 0;
        while (!localStorage.hasOwnProperty(this.key + id)) {
            id++;
        }
        return id;
    };  //返回下一个不为空的键值
    KeyStorage.prototype.add = function (obj) {
        localStorage.setItem(this.key + this.getNullID(), JSON.stringify(obj));
    };
    KeyStorage.prototype.getId = function (id) {
        localStorage.getItem(this.key + id);
    };
    KeyStorage.prototype.removeId = function (id) {
        localStorage.removeItem(this.key + id);
    }
    KeyStorage.prototype.clear = function () {
        for (var lkey in localStorage) {
            if (lkey.substr(0, this.key.length) === this.key) {
                try {
                    localStorage.removeItem(lkey);
                } catch (e) {

                }
            }
        }
    }
  window.KeyStorage=KeyStorage;
})()

//
k=new KeyStorage("num");  //localStorage ["num"+id]
k.add({}) //添加对象
 k.removeId(id);
k.forEach(function(o){console.log(o)});
k.clear()